//
//  MyClassResaveViewController.m
//  NewProject
//
//  Created by 李赐岩 姜雪 张昕锐 on 15/3/10.
//  Copyright (c) 2015年 李赐岩 姜雪 张昕锐. All rights reserved.
//

#import "MyClassResaveViewController.h"

@interface MyClassResaveViewController ()

@end

@implementation MyClassResaveViewController


-(void)viewWillAppear:(BOOL)animated
{
    self.segment.selectedSegmentIndex = 1;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.segment = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:@"四季菜谱收藏", @"分类菜谱收藏", nil]];
    self.segment.frame = CGRectMake(self.view.frame.size.width / 5, 15, self.view.frame.size.width * 3 / 5, 30);
    self.navigationItem.titleView = self.segment;
    [self.segment addTarget:self action:@selector(controlPresse:) forControlEvents:UIControlEventValueChanged];
    self.navigationController.navigationBar.tintColor = [UIColor colorWithRed:0 / 255.0 green:147/255.0 blue:52/ 255.0 alpha:0.9];;
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    
    self.tableView = [[UITableView alloc] initWithFrame:[[UIScreen mainScreen] bounds]style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    // Do any additional setup after loading the view.
}

-(void)setEditing:(BOOL)editing animated:(BOOL)animated
{
    [super setEditing:editing animated:animated];
    
    [self.tableView setEditing:editing animated:animated];
}

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return UITableViewCellEditingStyleDelete;
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    NewDetailModel *detail = [[[NewDetailDataBase shareDetailDataBaseHandle] selectDetail] objectAtIndex:indexPath.row];
    
    [[NewDetailDataBase shareDetailDataBaseHandle] deleteDetail:detail.number];
    
    [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationLeft];
    
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [[NewDetailDataBase shareDetailDataBaseHandle] selectDetail].count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        
    }
    
    cell.textLabel.text = [[[[NewDetailDataBase shareDetailDataBaseHandle] selectDetail] objectAtIndex:indexPath.row] title];
    return cell;
    
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    Reachability *wifi = [Reachability reachabilityForLocalWiFi];
    Reachability *conn = [Reachability reachabilityForInternetConnection];
    if ([wifi currentReachabilityStatus] == NotReachable || [conn currentReachabilityStatus] == NotReachable) {
        UIAlertView *alterView = [[UIAlertView alloc] initWithTitle:@"友情提示" message:@"网络不佳, 请稍后重试" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil];
        [alterView show];
    }

    DetailViewController *detailVC = [[DetailViewController alloc] init];
    
    detailVC.LabelStr3 = [[[[NewDetailDataBase shareDetailDataBaseHandle] selectDetail] objectAtIndex:indexPath.row] urlStr];
    detailVC.Labelstr1 = [[[[NewDetailDataBase shareDetailDataBaseHandle] selectDetail] objectAtIndex:indexPath.row] title];
    detailVC.LabelStr2 = [[[[NewDetailDataBase shareDetailDataBaseHandle] selectDetail] objectAtIndex:indexPath.row] imagestr];
    
    [self.navigationController pushViewController:detailVC animated:YES];
    
}


-(void)back
{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)controlPresse:(id)secder
{
    
    NSInteger segIndex = [self.segment selectedSegmentIndex];
    
    if (segIndex == 0) {
        CATransition *animation = [CATransition animation];
        [animation setDuration:1.0];
        animation.type = @"oglFlip";
        [animation setSubtype:kCATransitionFromLeft];
        [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
        
        [self.navigationController.view.layer addAnimation:animation forKey:nil];
        [self.navigationController popViewControllerAnimated:NO];
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
